<?php

if (!function_exists('auth')) {
    /**
     * Auth认证辅助方法
     * @param string|null $guard
     * @return \HyperfExt\Auth\Contracts\GuardInterface|\HyperfExt\Auth\Contracts\StatefulGuardInterface|\HyperfExt\Auth\Contracts\StatelessGuardInterface
     */
    function auth(string $guard = null)
    {
        if (is_null($guard)) $guard = config('auth.default.guard');
        return make(\HyperfExt\Auth\Contracts\AuthManagerInterface::class)->guard($guard);
    }
}

if (!function_exists('buildTree')) {
    /**
     * 生成树型列表
     * @param $data
     * @param string $children_key
     * @param string $prent_id_key
     * @param string $id_key
     * @param int $pid
     * @param int $level
     * @return array
     */
    function buildTree($data, string $children_key = 'children', string $prent_id_key = 'pid', string $id_key = 'id', int $pid = 0, int $level = 0): array
    {
        $dataTrees = [];
        if (empty($dataTrees)) {
            foreach ($data as $value) {
                if ($value[$prent_id_key] == $pid) {
                    $value[$children_key] = buildTree($data, $children_key, $prent_id_key, $id_key, $value[$id_key], $level + 1);
                    if (!$value[$children_key]) {
                        unset($value[$children_key]);
                    }
                    $dataTrees[] = $value;
                }
            }
        }
        return $dataTrees;
    }
}